package com.ys.controller.api.storage;

import com.alibaba.fastjson.JSON;
import com.ys.config.MenuId;
import com.ys.entity.base.TbCountry;
import com.ys.entity.storage.TccLogisticsDetailChannelFee;
import com.ys.entity.storage.TccWarehouse;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.storage.StorageMapper;
import com.ys.service.storage.TccLogisticsDetailChannelFeeService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-05
 * @Description: 物流渠道费用详情
 * @Version: 1.5
 */
@Controller
@RequestMapping("/api/inter/tccLogisticsDetailChannelFee")
public class ApiInterTccLogisticsDetailChannelFeeController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTccLogisticsDetailChannelFeeController.class);

    @Autowired
    private TccLogisticsDetailChannelFeeService tccLogisticsDetailChannelFeeService;

    @Autowired
    private CommonApiService<TccLogisticsDetailChannelFee> commonApiService;

    //菜单id
    private Long menuId = MenuId.TCC_LogisticsDetailChannelFee;

    @Autowired
    private StorageMapper storageMapper;
    /**
     * 查询
     */
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TccLogisticsDetailChannelFee cond, String conditions
    ){
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);
            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }



    /**
    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TccLogisticsDetailChannelFee setSeachSql(TccLogisticsDetailChannelFee cond){

        cond
                .setOrderby("A.CreateTime DESC");

        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }



    /*
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R add(HttpServletRequest request,@RequestParam Map<String, Object> params, Long id) {
        try {

            //修改
            return tccLogisticsDetailChannelFeeService.add( params, id);
        } catch (Exception e) {
            logger.error("物流方式维护新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R edit(HttpServletRequest request,@RequestParam Map<String, Object> params, Long id) {
        try {
            return tccLogisticsDetailChannelFeeService.save(params, id);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
    /**
     * 删除列表信息 - 运营公司表VPS信息
     */
    @RequestMapping("/del")
    @ResponseBody
    public R del(@RequestParam("list") List<String> idList){
        try {
            for(int i=0;i<idList.size();i++) {
                tccLogisticsDetailChannelFeeService.del(Long.parseLong(idList.get(i)));//删除
            }
            return R.ok();
        }catch (Exception e) {
            logger.error("删除出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccLogisticsDetailChannelFee cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond)
                    .addField("CASE WHEN A.TransportationId=0 THEN '快递' WHEN A.TransportationId=1 THEN '空运'" +
                            "WHEN A.TransportationId=2 THEN '卡航' WHEN A.TransportationId=3 THEN '铁路' " +
                            "WHEN A.TransportationId=4 THEN '海运' END  transportationName")
                    .addField("CASE WHEN A.NameLogsticsClearance=1 THEN '包税' WHEN A.NameLogsticsClearance=0 THEN '自税'" +
                            " END logsticsClearanceName");

            TccLogisticsDetailChannelFee tccLogisticsDetailChannelFee = JSON.parseObject(JSON.toJSONString(cond), TccLogisticsDetailChannelFee.class);
            TccWarehouse tccWarehouse = JSON.parseObject(JSON.toJSONString(cond), TccWarehouse.class);


            String transportationName = ConditionUtil.getConditionValue(conditions, "transportationId");//运输类型

            String nameLogsticsClearance = ConditionUtil.getConditionValue(conditions, "nameLogsticsClearance");//清关方式

            String logsticsWay = ConditionUtil.getConditionValue(conditions, "logsticsWay");//物流服务名称


            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(1)
                    .setFrontExport(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TccLogisticsDetailChannelFee> util = new ExportExcelUtils<>(new TccLogisticsDetailChannelFee());
            String toFilePath = util.exportExcel(listData, listField, "物流服务维护单");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }

}
